package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.Gift;
import cn.wolfcode.crm.domain.Member;
import cn.wolfcode.crm.query.GiftQueryObject;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.serivce.IGiftService;
import cn.wolfcode.crm.util.JSONResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("gift")
public class GiftController {
    @Autowired
    private IGiftService service;

    @RequestMapping("view")
    public String view() throws Exception {
        return "gift";
    }
    /*@RequestMapping("convert")
    public String convert() throws Exception {
        return "gift";
    }*/

    @RequestMapping("query")
    @ResponseBody
    public PageResult query(GiftQueryObject qo) throws Exception {
        PageResult query = service.query(qo);
        return query;
    }
    @RequestMapping("selectAll")
    @ResponseBody
    public List<Gift> selectAll() throws Exception {
        List<Gift> gifts = service.selectAll();
        return gifts;
    }
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    @RequiresPermissions(value={"Gift:saveOrUpdate","礼品新增/编辑"},logical = Logical.OR)
    public JSONResult saveOrUpdate(Gift gift) throws Exception {
        try {
            service.saveOrUpdate(gift);
            return new JSONResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JSONResult().mark("保存失败!");
        }
    }
    @RequestMapping("delete")
    @ResponseBody
    public JSONResult delete (Long id) throws Exception{
        try{
            service.deleteByPrimaryKey(id);
            return new JSONResult();
                }catch (Exception e){
                    e.printStackTrace();
                    return new JSONResult().mark("删除失败");
        }
    }
    @RequestMapping("updatePoint")
    @ResponseBody
    public JSONResult updatePoint(Gift gift) throws Exception {
        try {
            service.updatePoint(gift);
            return new JSONResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JSONResult().mark("操作失败!");
        }
    }
}
